package vip.zhenzicheng.websocket.netty_socketio.handler;

import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.annotation.OnConnect;
import com.corundumstudio.socketio.annotation.OnDisconnect;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 处理连接相关一些方式，例如连接建立和关闭的一些处理
 *
 * @author zhenzicheng
 * @date 2023-04-14 15:42
 */
@Component
@Slf4j
public class SocketConnection {

    public static ConcurrentHashMap<String, SocketIOClient> socketSessionMap = new ConcurrentHashMap<>();

    @OnConnect
    public void onConnect(SocketIOClient client) {
        log.info("client握手信息：{}", client.getHandshakeData());
        client.getHandshakeData();
        socketSessionMap.put(String.valueOf(client.getSessionId()), client);
        // client.leaveRoom();
    }

    @OnDisconnect
    public void onDisconnect(SocketIOClient client) {
        socketSessionMap.remove(String.valueOf(client.getSessionId()));
        log.info("断开连接");
    }

    public int getOnlineUserCount() {
        return socketSessionMap.size();
    }

    public ConcurrentHashMap<String, SocketIOClient> getConnectionInfoMap() {
        return socketSessionMap;
    }
}
